package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.ChannelEntity;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.model.MonitorEntity;
import com.whut.monitor.model.SensorEntity;
import com.whut.monitor.service.IMonitorService;
import com.whut.monitor.vo.MonitorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Sandeepin
 * 传感器监测服务层实现
 */
@Service
public class MonitorServiceImpl implements IMonitorService {

	@Autowired
	private IBaseDao<MonitorEntity> monitorDao;
	@Autowired
	private IBaseDao<EquipmentEntity> equipmentDao;
	@Autowired
	private IBaseDao<ChannelEntity> channelDao;

	@Override
	public void add(MonitorVo monitorVo) {
		List<MonitorEntity> list=new ArrayList<MonitorEntity>();
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, Object> eParams = new HashMap<String, Object>();
		Map<String, Object> cParams = new HashMap<String, Object>();
		eParams.put("equiptId", monitorVo.getEquipmentId());
		cParams.put("chantId", monitorVo.getChannelId());
		EquipmentEntity equipt=equipmentDao.findOne("from EquipmentEntity where id=:equiptId", eParams);
		params.put("equipt", equipt);
		ChannelEntity chant=channelDao.findOne("from ChannelEntity where id=:chantId", cParams);
		params.put("chant",chant);
		SensorEntity senr=new SensorEntity();
		senr.setId(monitorVo.getSensorId());
		params.put("senr", senr);
		String hql = "select count(*)  from MonitorEntity entity where entity.equipmentEntity=:equipt and entity.channelEntity=:chant"
				+ " and entity.sensorEntity=:senr";
		 int count=monitorDao.count(hql,params).intValue();
	     for(int i=0;i<monitorVo.getQuantity();i++){
			MonitorEntity entity = new MonitorEntity();
			String num=String.format("%02d", i+count);
			num=equipt.getEquipmentNum()+"-"+chant.getChannelNum()+"-"+num;
			entity.setMonitorNum(num);
			entity.setDescrip(monitorVo.getDescrip());
			entity.setEquipmentEntity(equipt);
			entity.setChannelEntity(chant);
			entity.setSensorEntity(senr);
			list.add(entity);
		}
		monitorDao.bathSave(list);
	}
	

	
	@Override
	public void delete(Integer id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		monitorDao.executeHql("delete from MonitorEntity entity where entity.id=:id",params);
	}

	
	@Override
	public void edit(MonitorVo monitorVo) {
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, Object> mparams = new HashMap<String, Object>();
		EquipmentEntity equipt=new EquipmentEntity();
		equipt.setId(monitorVo.getEquipmentId());
		params.put("equipt", equipt);
		ChannelEntity chant=new ChannelEntity();
		chant.setId(monitorVo.getChannelId());
		params.put("chant",chant);
		SensorEntity senr=new SensorEntity();
		senr.setId(monitorVo.getSensorId());
		params.put("senr", senr);
		String chql = "select count(*)  from MonitorEntity entity where entity.equipmentEntity=:equipt and entity.channelEntity=:chant"
				+ " and entity.sensorEntity=:senr";
		int count=monitorDao.count(chql,params).intValue();
		mparams.put("monitorNum",count);//编号从零开始
		mparams.put("equipt", equipt);
		mparams.put("chant",chant);
		mparams.put("senr", senr);
		mparams.put("descrip", monitorVo.getDescrip());
		mparams.put("id", monitorVo.getId());
		String hql="update MonitorEntity entity set entity.equipmentEntity=:equipt,entity.channelEntity=:chant"
				+ ",entity.sensorEntity=:senr,entity.descrip=:descrip,entity.monitorNum=:monitorNum where entity.id=:id";
		monitorDao.executeHql(hql,mparams);
         
	}
     
	
	@Override
	public List<MonitorVo> dataGrid(MonitorVo monitorVo, PageFilter pageFilter){
		List<MonitorVo> ul = new ArrayList<MonitorVo>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from MonitorEntity entity ";
		List<MonitorEntity> list = monitorDao.findList(hql + whereHql(monitorVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
		for (MonitorEntity entity : list) {
			MonitorVo u = new MonitorVo();
			BeanUtils.copyProperties(entity, u);
			
			u.setEquipmentId(entity.getEquipmentEntity().getId());
			u.setEquipmentName(entity.getEquipmentEntity().getEquipmentName());
			
			u.setChannelId(entity.getChannelEntity().getId());
			u.setChannelName(entity.getChannelEntity().getChannelName());
			
			u.setSensorId(entity.getSensorEntity().getId());
			u.setSensorName(entity.getSensorEntity().getSensorName());
			
			ul.add(u);
		}
		return ul;
	}
	
	
	@Override
	public Long count(MonitorVo monitorVo, PageFilter pageFilter) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from MonitorEntity entity ";
		return monitorDao.count("select count(*) " + hql + whereHql(monitorVo, params), params);
	}
	
	private String whereHql(MonitorVo monitorVo, Map<String, Object> params) {
		String hql = "";
		if (monitorVo != null) {
			hql += " where 1=1 ";
			if (monitorVo.getSensorName()!= null) {
				hql += " and entity.name like :name";
				params.put("name", "%%" + monitorVo.getSensorName()+ "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter pageFilter) {
		String orderString = "";
		if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
			orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
		}
		return orderString;
	}

	@Override
	public List<MonitorEntity> getMonitorByChannel(Integer channelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("channelId", channelId);
		List<MonitorEntity> list=monitorDao.findList("from MonitorEntity c where c.channelEntity.id=:channelId",params);
		return list;
	}

}

